<?php
declare (strict_types = 1);
namespace app\api\controller;

use app\LoginController;
use app\model\MyException;
use app\model\table\Ip;
use app\model\table\user\User;
use app\model\tablenot\tool\Tool;
use hg\apidoc\annotation as Apidoc;
use Ramsey\Uuid\Uuid;

/**
 * @Apidoc\Title("用户")
 */
class UserController extends LoginController
{
    /**
     * @Apidoc\Title("用户-获取token")
     * @Apidoc\Desc("获取token，相当于重新生成一个用户,丢失token，代表账号遗失,建议在用户第一次打开app的时候就调用这个接口生成一个账户")
     * @Apidoc\Method("GET")
     */
    public function getTokenAction()
    {
        $token = Uuid::uuid4();
        $postData = array(
            'token' => $token,
            'created_at' => $this->time
        );
        User::instance()->addOne($postData);
        $this->setData($token);
        $this->sendJson();
    }

    /**
     * @Apidoc\Title("用户-获取用户信息")
     * @Apidoc\Desc("1.每次打开游戏的时候调用一次")
     * @Apidoc\Method("GET")
     * @Apidoc\Returned("name", type="int", desc="昵称")
     * @Apidoc\Returned("gold", type="int", desc="金币")
     * @Apidoc\Returned("level", type="int", desc="通关级别")
     * @Apidoc\Returned("score", type="int", desc="分数，理论上通关级别=分数，为了后面扩展，所以用两个字段处理")
     * @Apidoc\Returned("score_season", type="int", desc="当前赛季分数")
     * @Apidoc\Returned("ip", type="int", desc="用户ip地址")
     * @Apidoc\Returned("nation", type="int", desc="用户所属国家，用来做本地排行榜")
     * @Apidoc\Returned("union_id", type="int", desc="工会id")
     * @Apidoc\Returned("help_num", type="int", desc="帮助别人次数")
     * @Apidoc\Returned("created_at", type="int", desc="创建时间")
     */
    public function infoAction()
    {
        // 每次获取
        $ip = Tool::getRealIp();
        $nation = Ip::instance()->getAddress($ip);
        $postData = array(
            'id' => $this->user['id'],
            'nation' => $nation,
            'last_login_time' => $this->time,
        );
        User::instance()->updateOne($postData);
        $this->setData($this->user);
        $this->sendJson();
    }

    /**
     * @Apidoc\Title("用户-修改用户信息")
     * @Apidoc\Desc("只支持用户名字修改,前端要判断一下，如果当前名字和修改后的名字是一样的，就不调用接口")
     * @Apidoc\Method("POST")
     * @Apidoc\Param ("name",require=true, type="int", desc="昵称")
     */
    public function updateAction()
    {
        Tool::checkParam(['name'],$this->post);
        $name = $this->post['name'];
        // 用户名字不能重复
        $UserObj = User::instance()->findOne(['name'=>$name], true);
        if(!empty($UserObj)){
            throw new MyException('昵称已存在');
        }
        $postData = array(
            'id' => $this->user['id'],
            'name' => $name
        );
        $UserObj = User::instance()->updateOne($postData);
        $this->setData($UserObj);
        $this->sendJson();
    }

    /**
     * @Apidoc\Title("用户-通关加分数")
     * @Apidoc\Desc("1. 如果用户离网通关100关，那么用户的关卡和分数会同步，但是赛季分数只会是+1")
     * @Apidoc\Method("POST")
     * @Apidoc\Param ("level", require=true, type="int", desc="关卡级别")
     */
    public function passLevelAction()
    {
        try {
            Tool::checkParam(['level'],$this->post);
            $level = $this->post['level'];

            $postData = array(
                'id' => $this->user['id'],
                'level' => $level,
                'score' => $level,
                'score_season' => $this->user['score_season'] + 1,
            );
            $UserObj = User::instance()->updateOne($postData);
            $this->setData($UserObj);
            $this->sendJson();
        } catch (MyException $e) {
            echo $e->toJson();
        }
    }
}
